package com.neu.questionnaireplus.config;


import com.neu.questionnaireplus.config.session.MySessionListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  /** 
  * @Description: 添加拦截器 
  * @Param: [registry] 
  * @return: void 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new VisiterInterceptor()).addPathPatterns("/**");
    super.addInterceptors(registry);
  }

  /** 
  * @Description: 允许跨域访问 
  * @Param: [registry] 
  * @return: void 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
        //.allowedOrigins("*")//.allowCredentials(false)
        .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
        .allowCredentials(true)
        .maxAge(3600)
        .allowedOriginPatterns("*")
        .allowedHeaders("*");
  }


  /** 
  * @Description: 注册session监听器 
  * @Param: [] 
  * @return: org.springframework.boot.web.servlet.ServletListenerRegistrationBean<com.neu.questionnaireplus.config.session.MySessionListener> 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  @Bean
  public ServletListenerRegistrationBean<MySessionListener> servletListenerRegistrationBean() {
    ServletListenerRegistrationBean<MySessionListener> slrBean = new ServletListenerRegistrationBean<>();
    slrBean.setListener(new MySessionListener());
    return slrBean;
  }


}

